package com.sky.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sky.constant.CategoryConstant;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.CategoryDTO;
import com.sky.dto.CategoryPageQueryDTO;
import com.sky.entity.Category;
import com.sky.entity.Setmeal;
import com.sky.exception.DishExistException;
import com.sky.exception.DishStatusException;
import com.sky.mapper.DishMapper;
import com.sky.mapper.PaginationMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.PaginationService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

/**
 * @projectName: sky-take-out
 * @package: com.sky.service.impl
 * @className: PaginationServiceImpl
 * @author: Eric
 * @description: TODO
 * @date: 2023/9/20 17:52
 * @version: 1.0
 */
@Service
public class PaginationServiceImpl implements PaginationService {
    @Autowired
    private PaginationMapper paginationMapper;

    @Autowired
    private DishMapper dishMapper;

    @Autowired
    private SetmealMapper setmealMapper;

    @Override
    public PageResult page(CategoryPageQueryDTO categoryPageQueryDTO) {
        PageHelper.startPage(categoryPageQueryDTO.getPage(),categoryPageQueryDTO.getPageSize());
        List<Category> paginationList = paginationMapper.selectByName(categoryPageQueryDTO.getName(),categoryPageQueryDTO.getType());
        PageInfo<Category> pageInfo = PageInfo.of(paginationList);
        return new PageResult(pageInfo.getTotal(),pageInfo.getList());
    }

    @Override
    public void addPagination(CategoryDTO categoryDTO) {
        Category category = new Category();
        category.setName(categoryDTO.getName());
        category.setType(categoryDTO.getType());
        category.setSort(categoryDTO.getSort());
        category.setStatus(StatusConstant.ENABLE);
        paginationMapper.addPagination(category);
    }

    @Override
    public void deleteClassificationById(Long id) {
        Category category = paginationMapper.selectById(id);
        if (Objects.equals(category.getType(), CategoryConstant.CATEGORY_TYPE_DISH)){
            if (Objects.equals(category.getStatus(),StatusConstant.ENABLE)){
                throw new DishStatusException(MessageConstant.DISH_ON_SALE);
            }
            Integer dishCount = dishMapper.getCountById(id);
            if (dishCount>0){
                throw new DishExistException(MessageConstant.CATEGORY_BE_RELATED_BY_DISH);
            }
        }else{
            if (Objects.equals(category.getStatus(),StatusConstant.DISABLE)){
                throw new DishStatusException(MessageConstant.SETMEAL_ON_SALE);
            }
            Integer setmealCount = setmealMapper.getCountById(id);
            if (setmealCount>0){
                throw new DishExistException(MessageConstant.DISH_BE_RELATED_BY_SETMEAL);
            }
        }
        paginationMapper.deleteClassificationById(id);
    }

    @Override
    public void modifyClassification(CategoryDTO categoryDTO) {
        Category category = new Category();
        BeanUtils.copyProperties(categoryDTO,category);
        paginationMapper.modifyClassification(category);
    }

    @Override
    public void changeStatus(Long id, Integer status) {
        Category category = paginationMapper.getCategoryById(id);
        category.setStatus(status);
        paginationMapper.modifyClassification(category);
    }

    @Override
    public List<Category> queryByType(Integer type) {
        List<Category> categoryList = paginationMapper.queryByType(type);
        return categoryList;
    }
}
